﻿
var seatViewText = '您选择的座位是：<br />';

var SEATS = {
    root: []
};
var SEATS_LINE = {
    root: []
};

//if (window.addEventListener) {
//    __firefox();
//}

function GetOS() {
    var OsObject = "";
    if (navigator.userAgent.indexOf("MSIE") > 0) {
        OsObject = "MSIE";
    }
    if (isFirefox = navigator.userAgent.indexOf("Firefox") > 0) {
        OsObject = "Firefox";
    }
    if (isSafari = navigator.userAgent.indexOf("Safari") > 0) {
        OsObject = "Safari";
    }

    return OsObject;
}

//----让火狐支持全局event
function __firefox() {
    HTMLElement.prototype.__defineGetter__("runtimeStyle", __element_style);
    window.constructor.prototype.__defineGetter__("event", __window_event);
    Event.prototype.__defineGetter__("srcElement", __event_srcElement);
}

function __element_style() {
    return this.style;
}

function __window_event() {
    return __window_event_constructor();
}

function __event_srcElement() {
    return this.target;
}

function __window_event_constructor() {
    if (document.all) {
        return window.event;
    }
    var _caller = __window_event_constructor.caller;
    while (_caller != null) {
        var _argument = _caller.arguments[0];
        if (_argument) {
            var _temp = _argument.constructor;
            if (_temp.toString().indexOf("Event") != -1) {
                return _argument;
            }
        }
        _caller = _caller.caller;
    }
    return null;
}


function getElementPos(elementId) {
    var ua = navigator.userAgent.toLowerCase();
    var isOpera = (ua.indexOf('opera') != -1);
    var isIE = (ua.indexOf('msie') != -1 && !isOpera); // not opera spoof   
    var el = document.getElementById(elementId);
    if (el.parentNode === null || el.style.display == 'none') {
        return false;
    }
    var parent = null;
    var pos = [];
    var box;
    if (el.getBoundingClientRect) //IE   
    {
        box = el.getBoundingClientRect();
        var scrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
        var scrollLeft = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);
        return {
            x: box.left + scrollLeft,
            y: box.top + scrollTop
        };
    }
    else
        if (document.getBoxObjectFor) // gecko       
        {
            box = document.getBoxObjectFor(el);
            var borderLeft = (el.style.borderLeftWidth) ? parseInt(el.style.borderLeftWidth) : 0;
            var borderTop = (el.style.borderTopWidth) ? parseInt(el.style.borderTopWidth) : 0;
            pos = [box.x - borderLeft, box.y - borderTop];
        }
        else // safari & opera       
        {
            pos = [el.offsetLeft, el.offsetTop];
            parent = el.offsetParent;
            if (parent != el) {
                while (parent) {
                    pos[0] += parent.offsetLeft;
                    pos[1] += parent.offsetTop;
                    parent = parent.offsetParent;
                }
            }
            if (ua.indexOf('opera') != -1 || (ua.indexOf('safari') != -1 && el.style.position == 'absolute')) {
                pos[0] -= document.body.offsetLeft;
                pos[1] -= document.body.offsetTop;
            }
        }
    if (el.parentNode) {
        parent = el.parentNode;
    }
    else {
        parent = null;
    }
    while (parent && parent.tagName != 'BODY' && parent.tagName != 'HTML') { // account for any scrolled ancestors   
        pos[0] -= parent.scrollLeft;
        pos[1] -= parent.scrollTop;
        if (parent.parentNode) {
            parent = parent.parentNode;
        }
        else {
            parent = null;
        }
    }
    return {
        x: pos[0],
        y: pos[1]
    };
}

function SetShowValue(obj, path) {

    if (path)
        obj.src = path;
    else
        obj.src = 'images/lock.gif';
    $('#seatid').val($('#seatid').val() + obj.id + ",");
    if (obj.title != '') {
        $('#seattip').val($('#seattip').val() + obj.title + " "); //title
        seatMoving(obj.title);
    }
    else {
        $('#seattip').val($('#seattip').val() + obj.alt + " "); //title
        seatMoving(obj.alt);
    }
}

function seatMoving(str) {
    var evt = (evt) ? evt : (window.event) ? window.event : "";
    var objTargElem = (evt.target) ? evt.target : evt.srcElement;
    var _objDiv = $("<div style='display:none;width:60px; height:18px;border:solid 1px orangered;position:absolute;background-color:#f3f3f3;color:orangered;z-index:100000;text-align:center;padding-top:4px;font-weight:bold;'></div>");
    //$(document.body).append(_objDiv);
    var objY = 0;
    var objX = 0;
    if (evt.pageX) {
        _objDiv[0].style.left = evt.pageX + "px";
        _objDiv[0].style.top = evt.pageY + "px";
    }
    else {
        objY = evt.clientY + document.documentElement.scrollTop + 2;
        objX = evt.clientX - 5;
        _objDiv[0].style.left = objX + "px";
        _objDiv[0].style.top = objY + "px";
    }
    _objDiv.html(str).fadeIn();
    var _ItemDiv = getElementPos('seatviewparent');
    move(_ItemDiv.x + 135, _ItemDiv.y + 40, _objDiv);
}

function move(aimLeft, aimTop, _objDiv) {
    var nowLeft = parseInt(_objDiv.css('left').replace('px', ''));
    var nowTop = parseInt(_objDiv.css('top').replace('px', ''));

    var leftLine = aimLeft - nowLeft;
    var topLine = nowTop - aimTop;
    var moveTime = 1;
    var feet = 30;
    if ($.browser.safari) {
        feet = 100;
    }
    /*var timer = window.setInterval(function(){
    nowLeft += leftLine / feet;
    nowTop -= topLine / feet;
    _objDiv.css('top', nowTop);
    _objDiv.css('left', nowLeft);
    if (nowTop <= aimTop) {
    window.clearInterval(timer);
    _objDiv.fadeOut();
            
    }
    }, moveTime);*/
    showValue();
}

function showValue() {
    var item = $('#seattip').val().split(' ');

    var inner = "";
    for (var i = 0; i < item.length; i++) {
        if ($.trim(item[i]) == "") {
            continue;
        }
        var mystr = "<span style='display:block; float:left;width:60px; height:18px;border:solid 1px orangered;background-color:#f3f3f3;color:orangered;z-index:100000;text-align:center;padding-top:2px;font-weight:bold;margin-right:12px;margin-top:2px;'>" + item[i] + "</span>";
        inner += mystr;
    }
    $("#seatview").html(seatViewText + inner);
}

function RemoveShowValue(obj, path) {
    if (path)
        obj.src = path;
    else
        obj.src = 'images/free.gif';
    $('#seatid').val($('#seatid').val().replace(obj.id + ",", ""));
    if (obj.title != '')
        $('#seattip').val($('#seattip').val().replace(obj.title + " ", ""));
    else
        $('#seattip').val($('#seattip').val().replace(obj.alt + " ", ""));

    showValue();
}



//遮盖已经锁的座位
function ShowLockSeats() {

    $.ajax({
        type: 'POST',
        url: 'lockseats.ashx',
        data: {
            "lsh": GetUrlParameter("lsh"), //电影编号
            "yth": GetUrlParameter("yth"), //影厅号
            "yyh": GetUrlParameter("yyh"), //影院编号
            "cc": GetUrlParameter("cc") //场次编号
        },
        success: function (msg) {

            if (msg == "-1") {
                alert("连接服务器获得锁座信息失败，请稍后再试。");
                return;
            }
            if (msg != "") {

                var seats = msg.split(",");

                for (var i = 0; i < seats.length; i++) {

                    $("#DivSeatShow").find("img").each(function () {

                        if ($(this).attr("title") == $.trim(seats[i])) {
                            $(this).attr("src", "/PhenixTicket/images/sale.gif");
                        }
                    });
                }

                //                var kk = $('#DivSeatShow img');
                //                var _seatIndex = 0;
                //                var _seatCol = 0;
                //                $.each(kk, function (i, item) {
                //                    var _itemid = parseInt(item.id.split(':')[0]);
                //                    var _col = parseInt(item.id.split(':')[1]);
                //                    if (_seatIndex < _itemid) {
                //                        _seatIndex = _itemid;
                //                        _seatCol = _col;
                //                    }
                //                });

                //                var lastSeatY = getElementPos(_seatIndex + ':' + _seatCol).y;
                //                var usedHeight = lastSeatY - $('#seatViewer').offset().top + 40;
                //                if (usedHeight > parseInt($('#seatViewer').css("height").replace('px', '')))
                //                    $('#seatViewer').css("height", usedHeight);
                //                var _w = document.getElementById("seatViewer").scrollWidth;
                //                
                //                if (_w <= 593)
                //                    return;

                //                $("#seatViewer").hScrollPane({
                //                    mover: "div",
                //                    moverW: function () {
                //                        return _w;
                //                    } (),
                //                    showArrow: true,
                //                    handleCssAlter: "draghandlealter",
                //                    mousewheel: {
                //                        bind: true,
                //                        moveLength: 100
                //                    }
                //                });

                //                $(".hScrollPane_draghandle").bind("mouseover", function () {
                //                    $("#DivSeatShow").stop();
                //                    $(this).stop();
                //                });


                //                var div_move_width = _w - $('#DivSeatShow').width();
                //                var scroll_move_width = $('.hScrollPane_dragbar').width() - $(".hScrollPane_draghandle").width();
                //                $("#DivSeatShow").animate({
                //                    left: -div_move_width
                //                }, 4000);
                //                $(".hScrollPane_draghandle").animate({
                //                    left: scroll_move_width
                //                }, 4000);
                //                $("#DivSeatShow").animate({
                //                    left: -div_move_width / 2
                //                }, 4000);
                //                $(".hScrollPane_draghandle").animate({
                //                    left: scroll_move_width / 2
                //                }, 4000);



                $("#hideseatnote").hide();

            }

        }
    })
}

//检测选座位是佛符合要求
function selectSeat(img, img1, loveid, arrsel, path) {

    if (arrsel.length > 5) {
        showAlert("最多选择5个座位！");
        return;
    }
    //普通座位
    if (path.indexOf("free.gif") > 0) {
        SetShowValue(img);
    }

    //情侣座位
    if (loveid == 1 || loveid == 2) {
        if (arrsel.length > 4) {
            showAlert("最多选择5个座位！");
            return;
        }
        SetShowValue(img, 'images/lock1.gif');

        var tem_id = img1.split(":");

        var tem_name;
        if (loveid == 1)
            tem_name = parseInt(tem_id[1]) + 1;
        else
            tem_name = parseInt(tem_id[1]) - 1;

        tem_name = tem_id[0] + ":" + tem_name;

        var zz = document.getElementById(tem_name);
        SetShowValue(zz, 'images/lock2.gif');
    }
}

//取消选定座位
function cancelSeat(path, loveid, img, img1) {
    if (path.indexOf("lock.gif") > 0) {
        RemoveShowValue(img, 'images/free.gif');
    }

    if (path.indexOf("lock1.gif") > 0 || path.indexOf("lock2.gif") > 0) {

        var tem_id = img1.split(":");
        var tem_name;
        if (loveid == 1)
            tem_name = parseInt(tem_id[1]) + 1;
        else
            tem_name = parseInt(tem_id[1]) - 1;
        tem_name = tem_id[0] + ":" + tem_name;
        var zz = document.getElementById(tem_name);
        if (loveid == 1) {
            RemoveShowValue(img, 'images/love1.gif');
            RemoveShowValue(zz, 'images/love2.gif');
        }
        else {
            RemoveShowValue(img, 'images/love2.gif');
            RemoveShowValue(zz, 'images/love1.gif');
        }
    }
}

//选座位
function Checkimg(img1, loveid) {

    var path = new String();
    path = document.getElementById(img1).src;
    img = document.getElementById(img1);
    var selected = $('#seatid').val(); //document.Form1.seatid.value;
    var arrsel = selected.split(',');
    if (path.indexOf("free.gif") > 0 || path.indexOf("love1.gif") > 0 || path.indexOf("love2.gif") > 0) {
        selectSeat(img, img1, loveid, arrsel, path);
    }
    else
        if (path.indexOf("lock.gif") > 0 || path.indexOf("lock1.gif") > 0 || path.indexOf("lock2.gif") > 0) {

            cancelSeat(path, loveid, img, img1)
        }

    TipView(); //座位信息的显示控制
}

//所选座位的提示
function TipView() {
    //funPcardImg();
    if ($.trim($('#seatid').val()) != "") {
        $('#div_seatnone').hide();
        $('#seatselect').show();
    }
    else {
        $('#seatselect').hide();
        $('#div_seatnone').show();
    }
}

// 检查手机号输入
function isPhone() {
    var str = $("#txt_Mobile").val();
    str = str.replace(/(^\s*)|(\s*$)/g, '');

    if (isNaN(str)) {
        showAlert("手机号码输入错误！");
        $('#txt_Mobile').focus();
        return false;
    }
    else
        if (str.length != 11) {
            showAlert("手机号码输入错误！");
            $('#txt_Mobile').focus();
            return false;
        }
        else {
            return true;
        }
}

//提交订单之前的检测
function beforeSeatSubmit(arrsel, selected) {

    var returnValue = 0;
    if (arrsel == null || arrsel.length < 1 || selected.length < 1) {

        showAlert("您还未选座位,请选择座位！");
        return false;
    }
    else
        if (arrsel.length > 6) {
            showAlert("每个订单影票最多不能超过5张!");
            return false;
        }
        else {
            for (var i = 0; i < arrsel.length; i++) {
                try {
                    var item = arrsel[i];
                    if ($.trim(item) != '') {

                        var row = parseInt(item.split(':')[0]);

                        var seat = $(document.getElementById(item));

                        var mySeatList = $("img[id][name^='" + row + ":']");
                        var t;
                        for (var xx = 0; xx < mySeatList.length; xx++) {
                            for (var yy = 0; yy < mySeatList.length; yy++) {
                                var x_id = parseInt($.trim(mySeatList[xx].id.split(':')[1]));
                                var y_id = parseInt($.trim(mySeatList[yy].id.split(':')[1]));
                                if (x_id < y_id) {
                                    t = mySeatList[xx];
                                    mySeatList[xx] = mySeatList[yy];
                                    mySeatList[yy] = t;
                                }
                            }
                        }

                        /*
                        var f_id = parseInt($.trim(mySeatList[0].id.split(':')[1]));
                        var b_id = parseInt($.trim(mySeatList[mySeatList.length-1].id.split(':')[1]));
                        if(f_id>b_id){
                        t=mySeatList[0];
                        mySeatList[0]=mySeatList[mySeatList.length-1];
                        mySeatList[mySeatList.length-1]=t;
                        }
                        for (var p = 0; p < mySeatList.length; p++) {
                        alert(mySeatList[p].id.split(':')[1]);
                        }
                        */


                        for (var j = 0; j < mySeatList.length; j++) {
                            if ($(mySeatList[j]).attr('id') == seat.attr('id')) {

                                var leftseat = null, leftseat1 = null, rightseat = null, rightseat1 = null;


                                try {
                                    leftseat = mySeatList[j - 1];
                                }
                                catch (ex) {
                                }

                                try {
                                    leftseat1 = mySeatList[j - 2];
                                }
                                catch (ex) {
                                }

                                try {
                                    rightseat = mySeatList[j + 1];
                                }
                                catch (ex) {
                                }

                                try {
                                    rightseat1 = mySeatList[j + 2];
                                }
                                catch (ex) {
                                }
                            }
                            if (leftseat != null && leftseat1 != null) {
                                if (leftseat.src.indexOf('free') > 0 && leftseat1.src.indexOf('free') < 0) {
                                    returnValue = 1;
                                }
                            }
                            else
                                if (leftseat != null && leftseat1 == null) {
                                    if (leftseat.src.indexOf('free') > 0) {
                                        returnValue = 2;
                                    }
                                }
                                else {
                                    if (rightseat != null && rightseat1 != null) {
                                        if (rightseat.src.indexOf('free') > 0 && rightseat1.src.indexOf('free') < 0) {
                                            returnValue = 3;
                                        }
                                    }
                                }

                            if (rightseat != null && rightseat1 != null) {
                                if (rightseat.src.indexOf('free') > 0 && rightseat1.src.indexOf('free') < 0) {
                                    returnValue = 4;
                                }
                            }
                            else
                                if (rightseat != null && rightseat1 == null) {
                                    if (rightseat.src.indexOf('free') > 0) {
                                        returnValue = 5;
                                    }
                                }
                                else {
                                    if (leftseat != null && leftseat1 != null) {
                                        if (leftseat.src.indexOf('free') > 0 && leftseat1.src.indexOf('free') < 0) {
                                            returnValue = 6;
                                        }
                                    }
                                }
                        }

                        if (returnValue > 0) {
                            showAlert('选座时，请尽量选连在一起的座位，不要留下单个的空闲座位！');
                            return false;
                        }
                    }
                }
                catch (ex) {

                }
            }
        }
    return true;
}


//选票验证并提交
function ticketCart() {

    var selected = $('#seattip').val(); //document.Form1.seatid.value;
    var selectedid = $('#seatid').val();
    var arrsel = selected.split(' ');
    var arrselid = selectedid.split(',');
    var sendvalue = "";
    var returnValue = beforeSeatSubmit(arrselid, selected);
    if (returnValue) {

        for (var i = 0; i < arrsel.length; i++) {
            if (arrsel[i] != "") {
                if (sendvalue == "") {
                    sendvalue = sendvalue + arrsel[i];
                }
                else {
                    sendvalue = sendvalue + "," + arrsel[i];
                }
            }
        }
        var RecMobile = $('#txt_Mobile').val(); ; //用于接收影票的手机号码

        if (RecMobile == "") {
            showAlert("请填写手机号！");
            $('#txt_Mobile').focus();
            return;
        }
        else {
            if (!isPhone()) {
                return;
            }
            else {
                $.ajax({
                    type: 'POST',
                    url: 'islogin.ashx',
                    success: function (date) {
                        if (date == "y") {
                            if (returnValue) {
                                var str = $('#tx_Filmindex').val() + "&row=" + sendvalue + "&mobile=" + RecMobile;
                                window.location.href = str;
                            }
                        }
                        else {
                            showLogin();
                            return;
                        }
                    }
                })
            }
        }
    }



}

function showloginnote(url) {
    $("#divshowlogin").show();
}

function notehide() {
    $("#divshowlogin").hide();
}

function showDiv(obj) {
    center(obj);
    $(window).scroll(function () {
        center(obj);
    });
    $(window).resize(function () {
        center(obj);
    });
}

function center(obj) {
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $(obj).height();

    $(obj).css({
        "position": "absolute",
        "top": (windowHeight - popupHeight) / 2 + $(document).scrollTop()
    });
}

function closeDiv() {
    window.close();
}

function showAlert(msg) {
    $.weeboxs.open(msg, {
        title: '网票网提示<font color="red">3</font>秒后自动关闭',
        type: 'alert',
        timeout: 3,
        onopen: function (box) {
            var closetime = parseInt(box.dt.find('font').html(), 10);
            var handle = setInterval(function () {
                closetime--;
                box.dt.find('font').html(closetime);
                if (closetime <= 0)
                    clearInterval(handle);
            }, 1000);
        }
    });
}

function showLogin() {

    $.jBox("iframe:login.aspx", {
        id: "login",
        title: "登录",
        width: 360,
        height: 350,
        buttons: { '关闭': true }
    });

}


function tsShow(tagid, tsid) {
    $(tsid).show();
}

function tsClose(tsType) {
    $(tsType).hide();
}

function GetUrlParameter(paramName) {

    var returnVal = "0";
    try {
        var paramUrl = window.location.search;
        //处理长度
        if (paramUrl.length > 0) {
            paramUrl = paramUrl.substring(1, paramUrl.length);
            var paramUrlArray = paramUrl.split("&");
            for (var i = 0; i < paramUrlArray.length; i++) {
                if (paramUrlArray[i].toLowerCase().indexOf(paramName.toLowerCase()) != -1) {
                    var temp = paramUrlArray[i].split("=");
                    if (temp[0].toLowerCase() == paramName.toLowerCase()) {
                        returnVal = temp[1];
                        break;
                    }
                }
            }
        }
    }
    catch (e) {
    }
    return returnVal;
}

$(function () {
    showDiv($("#ets"));
})
$(document).ready(function () {
    $(document).keydown(function (event) {
        if (event.keyCode == 13) {
            return false;
        }
    });
});

